package com.laborer.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.laborer.entity.Task;
import com.laborer.entity.User;
import com.laborer.entity.Video;
import com.laborer.entity.Weeks;
import com.laborer.entity.vo.ShowObject;
import com.laborer.model.result.Result;
import com.laborer.service.ITaskService;
import com.laborer.service.IUserService;
import com.laborer.service.IVideoService;
import com.laborer.service.IWeeksService;
import com.laborer.utils.StudentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.laborer.model.result.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/show")
public class ShowController {
    private final String PREFIX = "assignmentShow";
    @Autowired
    private IUserService iUserService;
    @Autowired
    private ITaskService iTaskService;
    @Autowired
    private IVideoService iVideoService;
    @Autowired
    private IWeeksService iWeeksService;

    @RequestMapping("")
    public String show() {
        return PREFIX + "/assignmentShow";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Object list() {
        //查询所有的用户
        List<User> userList = iUserService.list(null);
        //最终返回的是ShowObjectList
        List<ShowObject> showObjectList=new ArrayList<>(userList.size());
        for(int i=0;i<userList.size();i++){
            ShowObject showObject=new ShowObject();
            showObject.setId(userList.get(i).getId());
            showObject.setUserName(userList.get(i).getUserName());
            showObjectList.add(showObject);
        }

        //获取到本周的开始时间
        Weeks schedule = iWeeksService.getById("schedule");
        Date current = schedule.getCurrent();

        //查询所有的用户关联的任务
        if (userList != null && userList.size() > 0) {
            for (int i = 0; i < userList.size(); i++) {
                User user = userList.get(i);
                ShowObject showObject = showObjectList.get(i);

                //当前用户的所有视频量。
                long userCount=0;
                QueryWrapper<Task> queryTask = new QueryWrapper<>();
                queryTask.eq("user_id", user.getId());
                //每个用户关联的任务
                List<Task> taskList = iTaskService.list(queryTask);

                //当前用户本周的视频量
                int userLength=0;
                if (taskList != null && taskList.size() > 0) {
                    for (int j = 0; j < taskList.size(); j++) {
                        //查询每个任务下的视频集
                        Task task = taskList.get(j);
                        QueryWrapper<Video> queryVideo = new QueryWrapper<>();
                        queryVideo.eq("video_parent", task.getId());
                        List<Video> videoList = iVideoService.list(queryVideo);

                        //计算出 ->本周<- 此任务集的视频量
                        int taskLength = 0;
                        if (videoList != null && videoList.size() > 0) {
                            for (int son = 0; son < videoList.size(); son++) {
                                //当前任务集
                                Video video = videoList.get(son);
                                QueryWrapper<Video> queryVideoSon = new QueryWrapper<>();
                                queryVideoSon.eq("video_parent", video.getId());
                                queryVideoSon.ge("create_time", current);
                                List<Video> list = iVideoService.list(queryVideoSon);
                                //计算出 ->本周<- 此视频集的视频量
                                int videoLength = 0;
                                if (list != null && list.size() > 0) {
                                    for (int vc = 0; vc < list.size(); vc++) {
                                        //当前任务
                                        Video video1 = list.get(vc);
                                        videoLength += video1.getVideoLength();
                                    }
                                }
                                taskLength += videoLength;
                            }
                        }
                        userLength += taskLength;

                        //将当前任务的所有的视频集合计
                        long taskCount = 0;
                        if (videoList != null && videoList.size() > 0) {
                            for (int v = 0; v < videoList.size(); v++) {
                                Video video = videoList.get(v);
                                taskCount += video.getVideoLength();
                            }
                        }
                        //把当前任务的视频量添加到当前用户的视频量中。
                        userCount += taskCount;
                    }
                }

                showObject.setCountVideo(StudentUtils.intParseDate(userCount));
                showObject.setMondyVideo(StudentUtils.intParseDate(userLength));
            }
        }

        Result result = new Result();
        result.success(showObjectList,10,1);
        return result;
    }
}
